import {Component, OnInit} from '@angular/core';
import {App, NavController} from 'ionic-angular';
import {PersonPage} from '../person/person';
import {services} from "../../providers/service";
import {AddChildService} from "./addChild.service";
import {ChildPage} from "../child/child";
import {UserService} from "../../providers/user-service";
import {AppPopup} from "../../util/popup";
import {UserLogin} from "../../modules/app-user/user-login/user-login";

@Component({
    selector: 'addChild', templateUrl: 'addChild.html', providers: [AddChildService]
})
export class AddChildPage implements OnInit {
    hasClick = false;
    showAlert = true;
    num = 120;
    errorMsg = '';      //错误的提示信息
    childInfo = {
        "name": "", "mobile": ""
    }

    constructor(public navCtrl: NavController,
                private userServ: UserService,
                private popup: AppPopup,
                private app: App,
                public service: services,
                public addChildService: AddChildService) {
    }

    ionViewWillEnter() {
        this.userServ.getPersonInfo()
            .then(() => {
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        });
                }
            });
    }

    goback() {
        this.navCtrl.pop(PersonPage);
    }


    btnChoose() {
        this.showAlert = true;
        this.navCtrl.pop(PersonPage);
    }

    toAddChildAccount() {
        let regName = new RegExp('^[(\u4E00-\u9FA5)| (\u0061-\u007a) | (\u0041-\u005a) | (a-zA-Z) ]{2,8}$');
        if (this.childInfo.mobile == '' || this.childInfo.name == '') {
            this.errorMsg = "提交的信息不能为空";
        } else if (!regName.test(this.childInfo.name)) {
            this.errorMsg = "姓名控制2-8个字符，只能添加汉字和英文字母";
        } else if (!(/^1[34578]\d{9}$/.test(this.childInfo.mobile))) {
            this.errorMsg = "输入手机号格式不正确，请重新输入";
            // this.childInfo.mobile = '';
        } else {
            let param = {
                "name": this.childInfo.name, "mobile": this.childInfo.mobile
            }
            this.addChildService.addChildAccount(param)
                .then(data => {
                    console.log("添加子账号");
                    if (data.resultStatus == 'success') {
                        this.navCtrl.pop(ChildPage);
                    } else {
                        // this.addChildService.alertMsg(data.errorMassage);
                        this.errorMsg = data.errorMassage;
                    }

                }, err => {
                    console.log('失败回调');
                    return err;
                })
                .catch(fail => {
                    console.log('异常了', fail);
                });
        }
    }

    ngOnInit() {

    }

}
